package com.culturalyard.service.impl;

import com.culturalyard.constant.ManagementAreaStatusConstant;
import com.culturalyard.constant.ManagementAreaTopStatusConstant;
import com.culturalyard.constant.TopStatusConstant;
import com.culturalyard.mapper.CameraMapper;
import com.culturalyard.mapper.YardMapper;
import com.culturalyard.pojo.bean.GeoPoint;
import com.culturalyard.pojo.dto.CameraPageQueryDTO;
import com.culturalyard.pojo.entity.Camera;
import com.culturalyard.pojo.entity.Yard;
import com.culturalyard.pojo.vo.CameraPageVO;
import com.culturalyard.pojo.vo.CameraVO;
import com.culturalyard.pojo.vo.YardVO;
import com.culturalyard.service.CameraService;
import com.culturalyard.service.YardService;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class YardServiceImpl implements YardService {
    @Autowired
    private YardMapper yardMapper;
    @Autowired
    private CameraMapper cameraMapper;
    @Autowired
    private CameraService cameraService;

    private static String MANAGEMENT_AREA = "常时监控区";
    private static String MANAGEMENT_AREA_ID = "178794cb-f322-47f5-9bdb-951499b859f7";

    /**
     * 返回所有小院连同它的监控（置顶摄像头排在第一位）
     * @return
     */
    @Override
    public List<YardVO> listWithCameras() {
        List<YardVO> yardVOS = new ArrayList<>();
        CameraVO cameraVO;
        YardVO yardVO;

        // 查看常时监控区的所有摄像头
        YardVO managementArea = new YardVO();
        managementArea.setId(MANAGEMENT_AREA_ID);
        managementArea.setName(MANAGEMENT_AREA);
        managementArea.setLocation(new GeoPoint());
        managementArea.setChildren(cameraService.listManagementArea());
        yardVOS.add(managementArea);

        List<CameraVO> children1 = managementArea.getChildren();
        String topId = UUID.randomUUID().toString();
        if (children1.size()>0){
            children1.get(0).setCameraManagementAreaTopState(ManagementAreaTopStatusConstant.TOPPING);
            topId = children1.get(0).getId();
        }

        // 查询所有小院
        List<Yard> yards = yardMapper.selectAll();

        // 遍历小院查询其拥有的监控
        for (Yard yard:yards){
            yardVO = new YardVO();
            BeanUtils.copyProperties(yard,yardVO);

            List<Camera> cameras = cameraMapper.selectByYardId(yard.getId());
            List<CameraVO> children = new ArrayList<>();
            for (Camera camera : cameras){
                // 生成对应的VO对象
                cameraVO = new CameraVO();
                BeanUtils.copyProperties(camera, cameraVO);
                cameraVO.setParentName(yard.getName());
                children.add(cameraVO);

                if (cameraVO.getId() == topId){
                    cameraVO.setCameraManagementAreaTopState(ManagementAreaTopStatusConstant.TOPPING);
                }
            }
            yardVO.setChildren(children);

            // 加入结果
            yardVOS.add(yardVO);
        }
        return yardVOS;
    }

    /**
     * 返回小院的置顶摄像头
     * @param id
     * @return
     */
    @Override
    public CameraVO selectTopCamera(String id) {
        CameraPageQueryDTO cameraPageQueryDTO = new CameraPageQueryDTO();
        cameraPageQueryDTO.setCameraTopState(TopStatusConstant.TOPPING);
        cameraPageQueryDTO.setId(id);
        cameraPageQueryDTO.setPage(1);
        cameraPageQueryDTO.setSize(1);
        CameraPageVO cameraPageVO = cameraService.pageQuery(cameraPageQueryDTO);
        if (cameraPageVO.getTotal() == 0){
            // 该小院不存在置顶摄像头
            return null;
        }
        return (CameraVO) cameraPageVO.getRecords().get(0);
    }
}
